package com.lanqiao.gxnushop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanqiao.gxnushop.entity.Buyers;
import com.lanqiao.gxnushop.entity.Products;
import com.lanqiao.gxnushop.entity.Sellers;
import com.lanqiao.gxnushop.entity.Stores;
import com.lanqiao.gxnushop.service.ProductsService;
import com.lanqiao.gxnushop.service.StoresService;
import com.lanqiao.gxnushop.util.StringUtil;
import com.lanqiao.gxnushop.util.UUIdUtil;
import com.lanqiao.gxnushop.valueObject.Page;
import com.lanqiao.gxnushop.valueObject.ProductsF;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.io.File;
import java.util.ArrayList;

@Log4j
@RestController
public class ProductsController {
    @Autowired
    private ProductsService productsService;
    @Autowired
    private StoresService storesService;

    @Operation(summary = "添加商品")
    @Parameter(name = "products",description = "商品信息")
    @PostMapping("/productsCtrl/add")
    public boolean add(Products products){
        products.setProductId(UUIdUtil.getUserId());
        //判断店铺是否存在
        Stores stores = new Stores();
        stores.setStoreId(products.getStoreId());
        if(storesService.select(products.getStoreId()) != null) {
            return productsService.add(products);
        }else{
            return false;
        }
    }

    @Operation(summary = "通过商品Id删除商品")
    @Parameter(name = "productId",description = "商品Id")
    @DeleteMapping("/productsCtrl/delete")
    public boolean delete(String productId){
        return productsService.delete(productId);
    }
    @Operation(summary="根据商品Id查找商品")
    @Parameter(name="productId",description = "商品id")
    @GetMapping("/productsCtrl/select")
    public Products select(String productId){
        return productsService.findById(productId);
    }

  /*  @Operation(summary="根据商品信息（name,state,goodsCategory）模糊匹配商品")
    @Parameter(name="name",description = "商品信息")
    @GetMapping("/productsCtrl/selectByPro")
    public ArrayList<Products> selectByName(Products products){
        return productsService.findByProduct(products);
    }
*/
    @Operation(summary = "更新商品")
    @Parameter(name="products" ,description = "商品信息")
    @PutMapping("/productsCtrl/update")
    public boolean update(Products products){
        return productsService.update(products);
    }


    @Operation(summary = "查看自己 单个店铺 下的所有商品，可以根据商品信息（name,state,goodsCategory）模糊匹配商品")
    @Parameter(name="products",description = "商品信息")
    @GetMapping("/productsCtrl/selectByStoreId")
    public PageInfo<Products> selectByProducts(Products product, Page page){
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        ArrayList list = productsService.findByStoreId(product);
        return new PageInfo<Products>(list);
    }

    @Operation(summary = "查看所有商品，用于首页商品展示")
    @Parameter(name="page",description = "分页信息")
    @GetMapping("/productsCtrl/list")
    public PageInfo<Products> list(Page page){
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        ArrayList list = productsService.list();
        return new PageInfo<Products>(list);
    }

    /**
     * 查看所有商品，用于首页商品展示
     */
    @GetMapping("/productsCtrl/listAll")
    public ArrayList<Products> list() {
        return productsService.list();
    }


    @Operation(summary = "通过商品名字模糊查看商品， 用于首页  用户搜索商品  ")
    @Parameter(name = "name",description = "商品名称")
    @GetMapping("/productsCtrl/selectByName")
    public PageInfo<Products> selectByName(String name,Page page){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        ArrayList<Products> list = productsService.selectByName(name);
        return new  PageInfo<Products>(list);
    }

    @Operation(summary = "通过商品名字模糊查看商品， 用于首页  用户搜索商品 state = 1")
    @Parameter(name = "name",description = "商品名称")
    @GetMapping("/productsCtrl/selectByName2")
    public ArrayList<Products> selectByName2(String name){
        return productsService.selectByName2(name);
    }

    @Operation(summary = "根据分类查询商品")
    @Parameter(name = "name",description = "商品分类")
    @GetMapping("/productsCtrl/selectByGoodsCategory")
    public ArrayList<Products> selectByGoodsCategory(String name){
        return productsService.selectByGoodsCategory(name);
    }

    @Operation(summary = " 商家商品库存管理，可模糊查询商品名称name")
    @Parameter(name = "productsF",description = "商品信息,含卖家id")
    @GetMapping("/productCtrl/getStock")
    public PageInfo<Products> getStock(ProductsF productsF, Page page){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        ArrayList<Products> list = productsService.getStock(productsF);
        return new PageInfo<Products>(list);
    }



    @GetMapping("/productCtrl/getStock2")
    @Operation(summary = "商家商品库存管理，可模糊查询商品名称name,state,goodsCategory  && 某个买家下的所有商品 查询 ")
    public PageInfo<Products> getStock2(
            @RequestParam(value = "sellerId", required = false) String sellerId,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "state", required = false) String state,
            @RequestParam(value = "goodsCategory", required = false) String goodsCategory,
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize
    ) {
        ProductsF productsF = new ProductsF();
        productsF.setSellerId(sellerId);
        if(StringUtil.isNotEmpty(name)&& name != ""){
            productsF.setName(name);
        }
        if(StringUtil.isNotEmpty(state)&& state != ""){
            productsF.setState(state);
        }
        if(StringUtil.isNotEmpty(goodsCategory)&& goodsCategory != ""){
            productsF.setGoodsCategory(goodsCategory);
        }
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<Products> list = productsService.getStock(productsF);
        return new PageInfo<>(list);
    }

    @Operation(summary = "上传商品图片")
    @Parameter(name="pic",description = "用户头像图片" ,content = @Content(mediaType = "multipart/form-data")  )
    @Parameter(name="productId",description = "用户编号")
    @PostMapping("/productsCtrl/upload")
    public String upload(@RequestPart("pic") MultipartFile pic, @RequestParam("buyerId") String productId)  {
        String fileName = null;
        log.debug("--------------------------" + productId);
        try {
            if (!pic.isEmpty() && ImageIO.read(pic.getInputStream()) != null) {
                //获取文件名
                fileName = pic.getOriginalFilename();
                //通过UUID 设置新的文件名
                fileName = UUIdUtil.getUserId() + fileName.substring(fileName.lastIndexOf("."));
                //创建目标文件
                File file = new File(fileName);
                //将照片保存到目标文件
                pic.transferTo(file);

                //将图片名保存到数据库
                Products products = new Products();
                products.setProductId(productId);
                products.setImageUrl(fileName);
                log.debug("--------------------------" + products);
                productsService.updateImgUrl(products);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }
}
